<?php

namespace App\Http\Controllers\Api\V1;


use App\Models\Chapters;
use Illuminate\Http\Request;

class ChaptersController extends ApiController
{

    public function list(Chapters $chapters, Request $request)
    {
        $model = $chapters->newQuery();
        //获取单本目录
        if ($request->has('article_id') && !empty($request->article_id)) {
            $model->where('article_id', $request->article_id);
        }
        //排序
        if ($request->has('sort') && !empty($request->sort) && $request->sort == 1) {
            $model->orderBy('sort', 'asc');
        } else if ($request->has('sort') && !empty($request->sort) && $request->sort == 2) {
            $model->orderBy('sort', 'desc');
        }
        if (empty($request->input())) {
            $list = [];
        } else {
            $list = $model->where('status', 2)->get();
        }
        return $this->success('成功', $list);
    }

    public function info(Chapters $chapters, Request $request)
    {
        if (!$request->has('id') || empty($request->id)) {
            return $this->error('参数错误');
        }
        $data = Chapters::find($request->id);
        return $this->success('成功', $data);
    }

}
